﻿/***************************************************************************
 *  Project: WebControlSample
 *  File:    DownloadsForm.cs
 *  Version: 1.0.0.0
 *
 *  Copyright ©2012 Perikles C. Stephanidis; All rights reserved.
 *  This code is provided "AS IS" without warranty of any kind.
 *__________________________________________________________________________
 *
 *  Notes:
 *
 *  A Downloads content window displaying a list of active downloads.
 *   
 ***************************************************************************/

#region Using
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using WebControlSample.Properties;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections.Specialized;
#endregion

namespace WebControlSample
{
    partial class DownloadsForm : DockContent
    {
        #region Fields
        MainForm mainForm;
        #endregion

        #region Ctors
        public DownloadsForm( MainForm parentForm )
        {
            mainForm = parentForm;

            InitializeComponent();

            downloadCollectionBindingSource.DataSource = parentForm.Downloads;
            parentForm.Downloads.CollectionChanged += OnSourceCollectionChanged;
        }
        #endregion


        #region Methods
        protected override void OnDockStateChanged( EventArgs e )
        {
            base.OnDockStateChanged( e );

            if ( this.IsHidden )
                Settings.Default.Downloads = false;
            else if ( this.VisibleState != DockState.Unknown )
                Settings.Default.Downloads = true;
        }

        private Download GetSelectedDownload()
        {
            if ( downloadCollectionDataGridView.SelectedRows.Count == 0 )
                return null;

            return downloadCollectionDataGridView.SelectedRows[ 0 ].DataBoundItem as Download;
        }

        private void CancelSelectedDownload()
        {
            Download download = GetSelectedDownload();

            if ( ( download != null ) && download.IsDownloading )
            {
                if ( MessageBox.Show( this,
                        String.Format( "Are you sure you want to cancel downloading {0}?", download.FileName ),
                        "Awesomium.NET",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question ) == DialogResult.Yes )
                {
                    // Race condition.
                    if ( download.IsDownloading )
                    {
                        download.Cancel();
                        this.MainForm.Downloads.Remove( download );
                    }
                    else
                        MessageBox.Show( this, "Download was complete before your response.", "Awesomium.NET" );
                }
            }
        }
        #endregion

        #region Properties
        public MainForm MainForm
        {
            get
            {
                return mainForm;
            }
        }
        #endregion

        #region Event Handlers

        private void clearToolStripButton_MouseEnter( object sender, EventArgs e )
        {
            ( (ToolStripButton)sender ).ImageIndex += 1;
        }

        private void clearToolStripButton_MouseLeave( object sender, EventArgs e )
        {
            ( (ToolStripButton)sender ).ImageIndex -= 1;
        }

        private void OnSourceCollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
        {
            downloadCollectionBindingSource.ResetBindings( true );
        }

        private void downloadCollectionDataGridView_CellContentDoubleClick( object sender, DataGridViewCellEventArgs e )
        {
            if ( e.ColumnIndex != 1 )
                return;

            Download download = GetSelectedDownload();

            if ( ( download != null ) && download.CanOpenDownloadedFile() )
                download.OpenDownloadedFile();
        }

        private void downloadCollectionDataGridView_CellContentClick( object sender, DataGridViewCellEventArgs e )
        {
            if ( e.ColumnIndex != 5 )
                return;

            this.CancelSelectedDownload();
        }

        private void downloadsContextMenuStrip_Opening( object sender, CancelEventArgs e )
        {
            Download download = GetSelectedDownload();

            if ( download != null )
            {
                openContainingFolderToolStripMenuItem.Enabled = download.CanOpenDownloadedFile();
                cancelDownloadToolStripMenuItem.Enabled = download.IsDownloading;
                removeEntryToolStripMenuItem.Enabled = !download.IsDownloading;
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void openContainingFolderToolStripMenuItem_Click( object sender, EventArgs e )
        {
            Download download = GetSelectedDownload();

            if ( ( download != null ) && download.CanOpenDownloadedFile() )
                download.OpenDownloadedFileFolder();
        }

        private void cancelDownloadToolStripMenuItem_Click( object sender, EventArgs e )
        {
            this.CancelSelectedDownload();
        }

        private void removeEntryToolStripMenuItem_Click( object sender, EventArgs e )
        {
            Download download = GetSelectedDownload();

            if ( ( download != null ) && !download.IsDownloading )
                this.MainForm.Downloads.Remove( download );
        }

        private void clearToolStripButton_Click( object sender, EventArgs e )
        {
            if ( this.MainForm.Downloads.Count == 0 )
                return;

            List<Download> downloads = new List<Download>( this.MainForm.Downloads );

            foreach ( Download download in downloads )
            {
                if ( download.IsDownloadComplete || download.IsCancelled )
                    this.MainForm.Downloads.Remove( download );
            }
        }
        #endregion

    }
}
